//
//  DetailScorllView.m
//  Happy Kitchen
//
//  Created by mac_chen on 15/3/7.
//  Copyright (c) 2015年 mac_chen. All rights reserved.
//

#import "DetailScorllView.h"

@implementation DetailScorllView

- (instancetype)initWithFrame:(CGRect)frame detail:(DetailModel *)detail
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubviews:detail];
    }
    return self;
}
// 布局子视图
- (void)addSubviews:(DetailModel *)detail
{
    // 添加封面图片(菜谱图片)
    self.coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, self.frame.size.width - 10, self.frame.size.width - 20)];
//    self.coverImageView.backgroundColor = [UIColor redColor];
    
    // 利用第三方加载图片
    NSURL *url = [NSURL URLWithString:detail.Cover];
    [self.coverImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    
    [self addSubview:self.coverImageView];
    
    
    // 添加菜品名称
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.coverImageView.frame.origin.x, self.coverImageView.frame.size.height + self.coverImageView.frame.origin.y + 5, self.frame.size.width - 50, 30)];
    self.titleLabel.font = [UIFont systemFontOfSize:20];
    self.titleLabel.textColor = [UIColor cyanColor];
    
//    self.titleLabel.backgroundColor = [UIColor yellowColor];
    
    // 赋值
    self.titleLabel.text = detail.Title;
    
    [self addSubview:self.titleLabel];
    
    
    // 添加简介
    UILabel *intro = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, self.titleLabel.frame.size.height + self.titleLabel.frame.origin.y + 5, 100, 25)];
    intro.text = @"简介:";
    intro.textColor = [UIColor cyanColor];
    intro.font = [UIFont systemFontOfSize:18];
    [self addSubview:intro];
    
    // 添加简介文字
    self.introLabel = [[UILabel alloc] initWithFrame:CGRectMake(intro.frame.origin.x, intro.frame.size.height + intro.frame.origin.y + 5, self.coverImageView.frame.size.width, 50)];
    self.introLabel.font = [UIFont systemFontOfSize:15];
//    self.introLabel.backgroundColor = [UIColor yellowColor];
    self.introLabel.numberOfLines = 0;
    
    // 赋值
    self.introLabel.text = detail.Intro;
    // introLabel自适应高度
    CGRect introlFrame = self.introLabel.frame;
    introlFrame.size.height = [[self class] heightForText:detail.Intro];
    self.introLabel.frame = introlFrame;
    
    [self addSubview:self.introLabel];
    
    
    // 添加材料
    UILabel *stuff = [[UILabel alloc] initWithFrame:CGRectMake(self.introLabel.frame.origin.x, self.introLabel.frame.size.height + self.introLabel.frame.origin.y + 5, 100, 25)];
    stuff.text = @"材料清单:";
    stuff.textColor = [UIColor cyanColor];
    stuff.font = [UIFont systemFontOfSize:18];
    [self addSubview:stuff];
    
    
    // 初始化数组
    self.allStuffArr = [NSMutableArray array];
    // 将Stuff中的数据解析出来存到model中
    for (NSDictionary *dic in detail.Stuff) {
        StuffModel *stuffModel = [[StuffModel alloc] init];
        [stuffModel setValuesForKeysWithDictionary:dic];
        [self.allStuffArr addObject:stuffModel];
    }
    
    // 添加材料清单的数据
    for (NSInteger i = 0; i < self.allStuffArr.count; i++) {
        // 添加材料清单中的name数据
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(stuff.frame.origin.x + 10, stuff.frame.origin.y + stuff.frame.size.height + 20 * i + 5, 60, 18)];
        
        self.nameLabel.font = [UIFont systemFontOfSize:15];
        
        // 赋值
        StuffModel *stuffModel = [self.allStuffArr objectAtIndex:i];
        self.nameLabel.text = stuffModel.name;
        
//        self.nameLabel.backgroundColor = [UIColor yellowColor];
        [self addSubview:self.nameLabel];
        
        
        // 添加材料清单中的weight数据
        self.weightLabel = [[UILabel alloc] initWithFrame:CGRectMake(stuff.frame.origin.x + 60 + 10 + 5, stuff.frame.origin.y + stuff.frame.size.height + 20 * i + 5, 60, 18)];
        
        self.weightLabel.font = [UIFont systemFontOfSize:15];
        
        // 赋值
        self.weightLabel.text = stuffModel.weight;
        
        [self addSubview:self.weightLabel];
        
    }
    
    
    // 添加制作时间
    UILabel *cookTime = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.frame.origin.x - 10, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height + 5, 80, 25)];
    cookTime.text = @"制作时间:";
    cookTime.textColor = [UIColor cyanColor];
    cookTime.font = [UIFont systemFontOfSize:18];
    [self addSubview:cookTime];
    
    
    // 添加制作时间内容
    self.cookTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(cookTime.frame.origin.x + cookTime.frame.size.width + 10, cookTime.frame.origin.y, 100, 25)];
    self.cookTimeLabel.font = [UIFont systemFontOfSize:15];
    // 赋值
    self.cookTimeLabel.text = detail.CookTime;
    
    [self addSubview:self.cookTimeLabel];
    
    
    // 添加用餐人数
    UILabel *userCount = [[UILabel alloc] initWithFrame:CGRectMake(cookTime.frame.origin.x, cookTime.frame.origin.y + 25 + 5, 80, 25)];
    userCount.text = @"用餐人数:";
    userCount.textColor = [UIColor cyanColor];
    userCount.font = [UIFont systemFontOfSize:18];
    [self addSubview:userCount];
    
    
    // 添加用餐人数内容
    self.userCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(userCount.frame.origin.x + userCount.frame.size.width + 10, userCount.frame.origin.y, 100, 25)];
    self.userCountLabel.font = [UIFont systemFontOfSize:15];
    // 赋值
    self.userCountLabel.text = detail.UserCount;
    
    [self addSubview:self.userCountLabel];
    
    
    // 添加制作步骤
    UILabel *steps = [[UILabel alloc] initWithFrame:CGRectMake(userCount.frame.origin.x, userCount.frame.origin.y + 25 + 5, 80, 25)];
    steps.text = @"制作步骤:";
    steps.textColor = [UIColor cyanColor];
    steps.font = [UIFont systemFontOfSize:18];
    [self addSubview:steps];
    
    
    // 初始化数组
    self.allstepsArr = [NSMutableArray array];
    // 将Steps中的数据解析出来存到model中
    for (NSDictionary *dic in detail.Steps) {
        StepsModel *stepsModel = [[StepsModel alloc] init];
        [stepsModel setValuesForKeysWithDictionary:dic];
        [self.allstepsArr addObject:stepsModel];
    }
    
    // 添加制作步骤的数据
    for (NSInteger i = 0; i < self.allstepsArr.count; i++) {
        // 添加制作步骤中的StepPhoto数据
        self.stepPhotoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(steps.frame.origin.x + 40, steps.frame.origin.y + stuff.frame.size.height + 130 * i + 5, 120, 120)];
        
        // 赋值 (利用第三方进行图片加载)
        StepsModel *stepsModel = [self.allstepsArr objectAtIndex:i];
        NSURL *stepsUrl = [NSURL URLWithString:stepsModel.StepPhoto];
        [self.stepPhotoImageView sd_setImageWithURL:stepsUrl placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
        
        [self addSubview:self.stepPhotoImageView];
        
        
        // 添加制作步骤中的Intro数据
        self.stepsIntroLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.stepPhotoImageView.frame.origin .x + self.stepPhotoImageView.frame.size.width + 10, self.stepPhotoImageView.frame.origin.y, self.frame.size.width / 2 - 20, self.stepPhotoImageView.frame.size.height)];
        self.stepsIntroLabel.backgroundColor= [UIColor clearColor];
        self.stepsIntroLabel.font = [UIFont systemFontOfSize:15];
        self.stepsIntroLabel.numberOfLines = 0;
        
        // 赋值
        self.stepsIntroLabel.text = stepsModel.Intro;
        
        [self addSubview:self.stepsIntroLabel];
        
        
        // 添加制作步骤的步骤数
        self.stepsCount = [[UILabel alloc] initWithFrame:CGRectMake(self.stepPhotoImageView.frame.origin .x - 25, self.stepPhotoImageView.frame.origin.y + 45, 30, 30)];
        self.stepsCount.font = [UIFont systemFontOfSize:15];
        
        // 数组的下标加1就是步骤的数字
        self.stepsCount.text = [NSString stringWithFormat:@"%ld.", i + 1];
        
        [self addSubview:self.stepsCount];
        
    }
    
    
    
    // 设置scorllView的contentSize决定滑动的范围
    self.contentSize = CGSizeMake(self.frame.size.width, self.introLabel.frame.size.height + self.nameLabel.frame.size.height * self.allStuffArr.count + self.frame.size.width - 10 + self.stepPhotoImageView.frame.size.height * self.allstepsArr.count + 300);
    
}

// label自适应高度
+ (CGFloat)heightForText:(NSString *)text
{
    CGSize size = CGSizeMake(365, 2000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
